package cn.com.org.dao;

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisPoolBase {
	private static JedisPool jedisPool = null;
	static {
		String hostname = "127.0.0.1";
		int port = 27017;
		int max_total = 1024;
		int max_idle = 200;
		int max_wait = 200000;
		boolean test_on_borrow = true;
		String conigPath = "/config/db.properties";
		Properties prop = new Properties();

		try {
			InputStream intream = Object.class.getResourceAsStream(conigPath);
			prop.load(intream);
			hostname = prop.getProperty("redis_hostname");
			port = Integer.parseInt(prop.getProperty("redis_port"));
			max_total = Integer.parseInt(prop.getProperty("redis_max_total"));
			max_idle = Integer.parseInt(prop.getProperty("redis_max_idle"));
			max_wait = Integer.parseInt(prop.getProperty("redis_max_wait"));
			test_on_borrow = Boolean.parseBoolean(prop
					.getProperty("redis_test_on_borrow"));
			GenericObjectPoolConfig config = new GenericObjectPoolConfig();
			config.setMaxTotal(max_total);
			config.setMaxIdle(max_idle);
			config.setMaxWaitMillis(max_wait);
			config.setTestOnBorrow(test_on_borrow);
			jedisPool = new JedisPool(config, hostname, port);
		} catch (Exception e) {
			//e.printStackTrace();
			System.out.println(hostname);
			jedisPool=new JedisPool(hostname,port);
		}
	}
	public synchronized static Jedis getJedis() {
		try {
			if (jedisPool != null) {
				Jedis resource = jedisPool.getResource();
				return resource;
			} else {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}
	public static void returnResource(final Jedis jedis) {
		if (jedis != null) {
			jedisPool.returnResource(jedis);
		}
	}
	public static int pasetInt(String inputparam) {
		return 0;
	}
}
